
var app = getApp();
var sliderWidth = 187.5; // 需要设置slider的宽度，用于计算中间位置
var WxParse = require('../../wxParse/wxParse.js');
Page({
  data: {
    img_url: 'https://17fx.com/imgs/',
    list:[],
    splist:[],
    tabs: ["商品信息", "商家列表"],
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 0,
    goods_remark:null,
    goods_content:null,
    goodsid:0 ,
    goods_pic:[],
    is_share:0,
    goods_name:null,
    fenxiangshow:false,
    commission:0,
    goods_pic_ewm:null,
    goods_pic_bg:null,
    scrolltop:0,
    supplierslist:[],
    suppliers_img:null,
    suppliers_name: null,
    suppliers_phone: null,
    suppliers_contacts: null,
    suppliers_id:0,
    subscribe: 1
  },
  //跳转到关注页面
  guanzhupage: function () {
    wx.navigateTo({ url: '../index/guanzhu' });
  },
  //返回顶部
  scrolltop:function(){
    this.setData({
      scrolltop:0
    })
  },
  tabClick: function (e) {
    var that = this
    that.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });
  },
  onLoad: function (options) {
    //判断是否登录，全局变量是否为空
    if (getApp().globalData.openid == null) {
      //登录调用
      getApp().getUserInfo(function (userInfo, openid1) { })
    }
    
    console.log('toshangping传过来的goods_id参数' + options.id)
    console.log(options.uid)
    getApp().globalData.suid = options.uid
    // 生命周期函数--监听页面加载
    var that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
          sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
        });
      }
    });
    //创建一个dialog
    wx.showToast({
      title: '正在加载中...',
      icon: 'loading',
      duration: 10000
    });
    wx.request({
      url: 'https://17fx.com/weapp/api/goodsinfo.html',
      data: {
        id: options.id,
        userid: getApp().globalData.userid
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res)
        console.log(res.data)
        if (res.data[0].goods_remark != null){
          var str = res.data[0].goods_remark
          str = str.replace(/<br \/>/g, '\n')
          
        }

        if (res.data[0].goods_content != null){
          var tab = res.data[0].goods_content
          tab = tab.replace(/<[^<>]+?>/g, '');
        }
        if (res.data[0].goods_content != null){
          var article = res.data[0].goods_content;
          
        }

        WxParse.wxParse('goods_content', 'html', article, that, 5);
       
        console.log(str)
        that.setData({
          list: res.data,
          splist: res.data[0].supplierslist,
          goods_remark:str,

          //goods_content:tab1,

          goodsid: res.data[0].goods_id,
          is_share: res.data[0].is_share,
          goods_name: res.data[0].goods_name,
          goods_pic: res.data[0].goods_images_list,
          commission: res.data[0].commission,
          goods_pic_ewm: res.data[0].goods_qcodeimg,
          goods_pic_bg: res.data[0].goods_qcodeimg2,
          suppliers_img: res.data[0].suppliers.logo,
          suppliers_name: res.data[0].suppliers.suppliers_name,
          suppliers_phone: res.data[0].suppliers.suppliers_phone,
          suppliers_contacts: res.data[0].suppliers.suppliers_contacts,
          suppliers_id: res.data[0].suppliers_id,
          subscribe: getApp().globalData.subscribe
        });
        

        wx.hideToast();

      }
    });
  },
  onShow: function () {

  },
  onScroll: function (e) {
    if (e.detail.scrollTop > 100 && !this.data.scrollDown) {
      this.setData({
        scrollDown: true
      });
    } else if (e.detail.scrollTop < 100 && this.data.scrollDown) {
      this.setData({
        scrollDown: false
      });
    }
  },
  ///购买
  EventHandle: function (event) {
    wx.navigateTo({
      url: 'vouchers?id=' + event.currentTarget.dataset.gid,
      success: function (res) {
        // success
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  ///跳转到首页
  EventHandle1: function (event) {
    wx.switchTab({
      url: '../index/index'
    })
  },
  toPay: function () {
    wx.navigateTo({
      url: '../pay/pay',
      success: function (res) {
        // success
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },

  makePhoneCall: function () {
    wx.makePhoneCall({
      phoneNumber: '13553835046' //仅为示例，并非真实的电话号码
    })
  },

  //分享
  onShareAppMessage: function (event) {
    //var goods_id = event.currentTarget.dataset.gid
    var goods_id = this.data.goodsid
    return {
      title: this.data.goods_name,
      desc: '最具人气的分享商城!',
      path: '/pages/detail/shangping?id=' + goods_id + '&uid=' + getApp().globalData.userid
    }
  },

  //分享
  isonShare: function (event) {
    
    if (this.data.fenxiangshow == false){
      this.setData({
        fenxiangshow:true
      })
    }else{
      this.setData({
        fenxiangshow:false
      })
    }

  },

  //一键保存图片和复制文字
  yijiansave:function(event){
    console.log('一键保存');
    //console.log(this.data.goods_remark);
    //console.log(this.data.goods_pic);
    //设置系统剪贴板的内容
    wx.setClipboardData({
      data: this.data.goods_remark,
    })
    // 可以通过 wx.getSetting 先查询一下用户是否授权了 "scope.writePhotosAlbum" 这个 scope
    wx.getSetting({
      success(res) {
        if (!res['scope.writePhotosAlbum']) {
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success() {
              // 用户已经同意小程序使用录音功能，后续调用 wx.startRecord 接口不会弹窗询问
              wx.saveImageToPhotosAlbum()
            }
          })
        }
      }
    })
    //生成的带二维码商品图片
    var picurl = 'https://17fx.com/' + this.data.goods_pic_bg;
      wx.getImageInfo({
        src: picurl,
        success: function (res) {
          console.log(res.width)
          console.log(res.height)
          console.log(res.path)
          wx.saveImageToPhotosAlbum({
            filePath: res.path,
            success(res) {
              console.log(res);
            },
            fail: function () {
              console.log('fail');
            },
            complete: function () {
            }
          })
        }
      })

    
    wx.showToast({
      title: '一键保存图片成功',
      icon: 'success',
      duration: 3000
    })

  },

  //推荐好友赚钱
  tjhyzjf:function(event){
    var coid = this.data.commission
    wx.showToast({
      title: '若您转发此链接到朋友圈或群，有朋友购买并到商家核销以后，您将即刻得到'+coid+'元返利',
      icon: 'loading',
      duration: 8000
    })
  },
  //拨打电话
  makePhoneCall: function (event) {
    //console.log('点击事件primary1,id:' + event.currentTarget.dataset.id )
    wx.makePhoneCall({
      phoneNumber: event.currentTarget.dataset.id //仅为示例，并非真实的电话号码
    })
  },
  //跳转到门店
  tomendian:function(event){
    var id = event.currentTarget.dataset.id
    wx.navigateTo({
      url: 'detail?id='+id
    })
  }



});